function g(url, div_res,div_msg,div_load,sinc){
	//Imagen Loading
	if (isSet(div_load)){
		loading(div_load);
		//console.log('muestra imagen');
	}

	if(contiene(url, '.php') && !contiene(url, '.php?')){
		url = url.replace('.php','.php?');
	}
	
	url = url.replace('.php?&','.php?');
	url = url.replace(/.php\?$/,'.php');

	$.ajax({
		url: url,
		dataType: 'json',
		async: sinc,
		success:function(data){
			if(isSet(div_res))
				if(isSet(data.html))
					if(hasValue(data.html))
						//console.log("Llega");
						$('#'+div_res).html(data.html);
			if(isSet(div_msg)){
				if(hasValue(data.msg))
					showMsg(div_msg,data.msg,data.type);
				else
					$('#'+div_msg).html('');
			}
	}}).done(function(){		
				if (isSet(div_load))
					finish(div_load);
	}).fail(function(){
			showMsg(div_msg,'Se ha producido un error. Intente nuevamente','e');
		});
}

function ps(url, div_res,div_msg,div_load,sinc){
	//Imagen Loading
	if (isSet(div_load)){
		loading(div_load);
		//console.log('muestra imagen');
	}

	if(contiene(url, '.php') && !contiene(url, '.php?')){
		url = url.replace('.php','.php?');
	}
	
	url = url.replace('.php?&','.php?');
	url = url.replace(/.php\?$/,'.php');

	$.ajax({
		url: url,
		type: "POST",
		dataType: 'json',
		async: sinc,
		success:function(data){
			if(isSet(div_res))
				if(isSet(data.html))
					if(hasValue(data.html))
						//console.log("Llega");
						$('#'+div_res).html(data.html);
			if(isSet(div_msg)){
				if(hasValue(data.msg))
					showMsg(div_msg,data.msg,data.type);
				else
					$('#'+div_msg).html('');
			}
	}}).done(function(){		
				if (isSet(div_load))
					finish(div_load);
	}).fail(function(){
			showMsg(div_msg,'Se ha producido un error. Intente nuevamente','e');
		});
}

function a(action){
	return '&action='+action;
}

function c(par,value){
	//console.log()
	return '&'+par+'='+value;
}

function v(variable){
	return '&'+variable+'='+$('#'+variable).val();
}

function recargar(){
	g(Sys.g.url, Sys.g.contenedor);
}

function getUrlPaginador(posicion){
	return Sys.paginador.url;
}

function contiene(cadena, busqueda){
	return cadena.indexOf(busqueda) != -1;
}

function l(link){
	window.location.href = link;
}

function p(url){
	window.open(url, '_blank');
	return false;
}

function loading(div_load){
	$('#'+div_load).html('<img class="img-loading" src="../img/loading.gif" />');
}

function finish(div_load){
	$('#'+div_load).html('');
}

function isEmpty(element){
	if(element == undefined || element == null || $('#'+element).val() == '')
		return true;
	else
		return false;
}

function hasMsg(element){
	if ($('#'+element).html() != '')
		return true;
	else
		return false;
}

function isSet(element){
	if(element == undefined || element == null )
		return false;
	else
		return true;
}

function hasValue(element){
	if(element == undefined || element == null || element == '')
		return false;
	else
		return true;
}

function showMsg(div_msg,text_msg,type){
	switch (type){
		case 'a':
			$('#'+div_msg).html('<img src="../img/symbol/symbol_exclamation.png" class="img-msg"/><div class="alert alert-warning alert-custom"> '+text_msg+'</div>');
			break;
		case 'o':
			$('#'+div_msg).html('<img src="../img/symbol/symbol_check.png" class="img-msg"/><div class="alert alert-success alert-custom"> '+text_msg+'</div>');
			break;
		case 'e':
			$('#'+div_msg).html('<img src="../img/symbol/symbol_error.png" class="img-msg"/><div class="alert alert-danger alert-custom"> '+text_msg+'</div>');
			break;
		case 'i':
			$('#'+div_msg).html('<img src="../img/symbol/symbol_information.png" class="img-msg"/><div class="alert alert-info alert-custom"> '+text_msg+'</div>');
			break;
		default:
			$('#'+div_msg).html('<img src="../img/symbol/symbol_exclamation.png" class="img-msg"/><div class="alert alert-info alert-custom"> '+text_msg+'</div>');
			break;
	}
	$('#'+div_msg).show();
}

function validChrField(field,minLength){
	if (isEmpty(field))
		return false;
	if ("$(#'"+field+"').length" <= minLength)
		return false;
	return true;
}

function validMailField(field){
	if (isEmpty(field))
		return false;
	if($("#"+field).val().indexOf('@', 0) == -1 || $("#"+field).val().indexOf('.', 0) == -1)
        return false;
    return true;
}

function encriptar(cadena){
	var result = md5('$2a$07$firstclassapartmentscordoba$'+cadena);
	return result;
}

function lpad(originalstr, length, strToPad) {
    while (originalstr.length < length)
        originalstr = strToPad + originalstr;
    return originalstr;
}

function hexc(colorval) {
    var parts = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    delete(parts[0]);
    for (var i = 1; i <= 3; ++i) {
        parts[i] = parseInt(parts[i]).toString(16);
        if (parts[i].length == 1) parts[i] = '0' + parts[i];
    }
    return '#' + parts.join('');
}

function sumarDiasFecha(dias,fecha){
    milisegundos=parseInt(35*24*60*60*1000);
    
    day=fecha.getDate();
    // el mes es devuelto entre 0 y 11
    month=fecha.getMonth()+1;
    year=fecha.getFullYear();
    
    //document.write("Fecha actual: "+day+"/"+month+"/"+year);
    
    //Obtenemos los milisegundos desde media noche del 1/1/1970
    tiempo=fecha.getTime();
    //Calculamos los milisegundos sobre la fecha que hay que sumar o restar...
    milisegundos=parseInt(day*24*60*60*1000);
    //Modificamos la fecha actual
    total=fecha.setTime(tiempo+milisegundos);
    day=fecha.getDate();
    month=fecha.getMonth()+1;
    year=fecha.getFullYear();

    return day + '/' + month + '/' + year;
 }

function addDays(date, days) {
    var result = new Date(date);
    result.setDate(date.getDate() + days);
    return result;
}